package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }

    @PostMapping("/user-group")
    public  String  saveUserGroup(@RequestBody UserGroup userGroup){

        userGroup.setConditionComment(userGroup.conditionJsonToComment());
        userGroup.setConditionJsonStr( JSON.toJSONString(userGroup.getTagConditions()) ); //fastjson把任何对象转换成json
        userGroup.setCreateTime(new Date());
        userGroupService.saveOrUpdate(userGroup);
        userGroupService.genUserGroupUids(userGroup);

        return  "success";
    }


    @PostMapping("/user-group-evaluate")
    public Long evaluate(@RequestBody UserGroup userGroup){
        Long uidCount= userGroupService.countUserGroupUids(userGroup);
        return  uidCount;
    }

    @PostMapping("/user-group-refresh/{id}")
    public String refresh(@RequestParam("busiDate")String busiDate, @PathVariable("id") String userGroupId){
        // 1 要做人群包 clickhouse 删掉 重新写入
        // 2 分群的基本信息 mysql 要做更新( 最后更新时间 ，人数）
        userGroupService.refreshUserGroupUids(userGroupId,busiDate);
        return "success";


    }


}

